#########################################
# Setup
#########################################

rm(list = ls())
setwd(".")
# Libraries
library(tidyverse)
library(modelsummary)

# Read data
survey <- read.csv("survey_data.csv") 

# Subset survey data to incumbent-aligned & opposition-aligned conditions
survey_inc <- survey %>% filter(condition %in% c(1, 2, 3, 4)) # incumbent-aligned
survey_opp <- survey %>% filter(condition %in% c(5, 6, 7, 8)) # opposition-aligned


# Clean survey data
survey_inc = survey_inc  %>%
  # Recode NA (non-adjusted) beliefs to prior
  mutate(prop_belief = case_when(is.na(prop_belief) == T ~ 50,
                                 .default = prop_belief),
         opp_belief = case_when(is.na(opp_belief) == T ~ 80,
                                .default = opp_belief)) %>%
  # Encode treatment conditions
  mutate(reform = case_when(condition %in% c(1,3) ~ 1,
                            condition %in% c(2,4) ~ 0),
         aligned = case_when(condition %in% c(1,2) ~ 0,
                             condition %in% c(3,4) ~ 1))

survey_opp <- survey_opp %>%
  # Recode NA (non-adjusted) beliefs to prior
  mutate(prop_belief = case_when(is.na(prop_belief) == T ~ 50,
                                 .default = prop_belief),
         opp_belief = case_when(is.na(opp_belief) == T ~ 80,
                                .default = opp_belief)) %>%
  # Encode treatment conditions
  mutate(reform = case_when(condition %in% c(5,7) ~ 1,
                            condition %in% c(6,8) ~ 0),
         unaligned = case_when(condition %in% c(5,6) ~ 0,
                               condition %in% c(7,8) ~ 1))


#########################################
# Table 2
#########################################

lm1 <- lm(data = survey_inc %>%
           filter(mvc_correct == 2), prop_belief/100 ~ reform* aligned)
lm1_cov <-  lm(data = survey_inc %>%
                filter(mvc_correct == 2), prop_belief/100 ~ reform* aligned +
                age +female + white + college)
lm2 <- lm(data = survey_inc%>%
           filter(mvc_correct == 2), opp_belief/100 ~ reform* aligned)
lm2_cov <- lm(data = survey_inc %>%
               filter(mvc_correct == 2), opp_belief/100 ~ reform* aligned+
               age +female + white + college)

tab_2 <- modelsummary(list("Competence" = lm1,
                  "Competence" = lm1_cov,
                  "Veto" = lm2,
                  "Veto" = lm2_cov),
             coef_map = c("reform" = "Reform",
                          "aligned" = "Incumbent",
                          "reform:aligned" = "Reform * Incumbent",
                          "(Intercept)" = "Constant" ),
             gof_map = c("nobs", "r.squared"),
             stars = TRUE)


print(tab_2)


#########################################
# Table 3
#########################################

lm3 <- lm(data = survey_opp %>%
           filter(mvc_correct == 2), prop_belief/100 ~ reform* unaligned)
lm3_cov <-  lm(data = survey_opp %>%
                filter(mvc_correct == 2), prop_belief/100 ~ reform* unaligned +
                age +female + white + college)
lm4 <- lm(data = survey_opp %>%
           filter(mvc_correct == 2), opp_belief/100 ~ reform* unaligned)

lm4_cov <- lm(data = survey_opp %>%
               filter(mvc_correct == 2), opp_belief/100 ~ reform* unaligned+
               age +female + white + college)

tab_3 <- modelsummary(list("Competence" = lm3,
                  "Competence" = lm3_cov,
                  "Veto" = lm4,
                  "Veto" = lm4_cov),
             coef_map = c("reform" = "Reform",
                          "unaligned" = "Opposition",
                          "reform:unaligned" = "Reform * Opposition",
                          "(Intercept)" = "Constant" ),
             gof_map = c("nobs", "r.squared"),
             stars = TRUE)

print(tab_3)


#########################################
# Appendix J - Table J1 
#########################################

survey_inc <- survey_inc %>% 
  mutate(inc_learn = case_when(reform == 1 & prop_belief > 50 & prop_belief <= 60 ~ 1,
                               reform == 0 & prop_belief < 50 & prop_belief >= 48.9 ~ 1,
                               is.na(prop_belief) == TRUE ~ NA_real_,
                               .default = 0)) %>%
  mutate(opp_learn = case_when(reform == 1 & opp_belief < 80  & opp_belief >= 0 ~ 1,
                               reform == 0 & opp_belief <= 89 & opp_belief > 80  ~ 1,
                               is.na(opp_belief) == TRUE ~ NA_real_,
                               .default = 0) )

lm5 <- lm(data = survey_inc %>%
           filter(mvc_correct == 2), inc_learn ~ reform * aligned)
lm5_cov <-  lm(data = survey_inc %>%
                filter(mvc_correct == 2), inc_learn ~ reform * aligned +
                age +female + white + college)
lm6 <- lm(data = survey_inc%>%
           filter(mvc_correct == 2), opp_learn ~ reform * aligned)

lm6_cov <- lm(data = survey_inc%>%
               filter(mvc_correct == 2), opp_learn ~ reform * aligned+
               age +female + white + college)

tab_J1 <- modelsummary(list("Competence" = lm5,
                  "Competence" = lm5_cov,
                  "Veto" = lm6,
                  "Veto" = lm6_cov),
             coef_map = c("reform" = "Reform",
                          "aligned" = "Incumbent",
                          "reform:aligned" = "Reform * Incumbent"),
             gof_map = c("nobs", "r.squared"),
             stars = TRUE)

print(tab_J1)


#########################################
# Appendix J - Table J2
#########################################

survey_opp <- survey_opp %>% 
  mutate(inc_learn = case_when(reform == 1 & prop_belief > 50 & prop_belief < 60 ~ 1,
                               reform == 0 & prop_belief < 50 & prop_belief > 48.9 ~ 1,
                               is.na(prop_belief) == TRUE ~ NA_real_,
                               .default = 0)) %>%
  mutate(opp_learn = case_when(reform == 1 & opp_belief < 80  & opp_belief > 0 ~ 1,
                               reform == 0 & opp_belief < 89 & opp_belief > 80  ~ 1,
                               is.na(opp_belief) == TRUE ~ NA_real_,
                               .default = 0) )

lm7 <- lm(data = survey_opp %>%
           filter(mvc_correct == 2), inc_learn ~ reform* unaligned)
lm7_cov <-  lm(data = survey_opp %>%
                filter(mvc_correct == 2), inc_learn ~ reform* unaligned +
                age +female + white + college)
lm8 <- lm(data = survey_opp %>%
           filter(mvc_correct == 2), opp_learn ~ reform* unaligned)

lm8_cov <- lm(data = survey_opp %>%
               filter(mvc_correct == 2), opp_learn ~ reform* unaligned+
               age +female + white + college)

tab_J2 <- modelsummary(list("Competence" = lm7,
                  "Competence" = lm7_cov,
                  "Veto" = lm8,
                  "Veto" = lm8_cov),
             coef_map = c("reform" = "Reform",
                          "unaligned" = "Opposition",
                          "reform:unaligned" = "Reform * Opposition" ),
             gof_map = c("nobs", "r.squared"),
             stars = TRUE)


print(tab_J2)

#########################################
# Appendix K - Table K1
#########################################

survey_inc <- survey_inc %>% 
  mutate(inc_dev = case_when(reform == 1 ~ abs(prop_belief - 60),
                             reform == 0 ~ abs(prop_belief - 48.9),
                             .default = NA_real_)) %>%
  mutate(opp_dev = case_when(reform == 1 ~ abs(opp_belief - 0),
                             reform == 0 ~ abs(opp_belief - 89),
                             .default = NA_real_) )


lm9 <- lm(data = survey_inc %>%
           filter(mvc_correct == 2), inc_dev ~ reform* aligned)
lm9_cov <-  lm(data = survey_inc %>%
                filter(mvc_correct == 2), inc_dev ~ reform* aligned +
                age +female + white + college)
lm10 <- lm(data = survey_inc%>%
           filter(mvc_correct == 2), opp_dev ~ reform* aligned)

lm10_cov <- lm(data = survey_inc%>%
               filter(mvc_correct == 2), opp_dev ~ reform* aligned+
               age +female + white + college)
options("modelsummary_format_numeric_latex" = "plain")


table_K1 <- modelsummary(list("Competence" = lm9,
                  "Competence" = lm9_cov,
                  "Veto" = lm10,
                  "Veto" = lm10_cov),
             coef_map = c("reform" = "Reform",
                          "aligned" = "Incumbent",
                          "reform:aligned" = "Reform * Incumbent" ),
             gof_map = c("nobs", "r.squared"),
             stars = TRUE)

print(table_K1)


#########################################
# Appendix K - Table K2
#########################################

survey_opp <- survey_opp %>% 
  mutate(inc_dev = case_when(reform == 1 ~ abs(prop_belief - 60),
                             reform == 0 ~ abs(prop_belief - 48.9),
                             .default = NA_real_)) %>%
  mutate(opp_dev = case_when(reform == 1 ~ abs(opp_belief - 0),
                             reform == 0 ~ abs(opp_belief - 89),
                             .default = NA_real_) )


lm11 <- lm(data = survey_opp %>%
           filter(mvc_correct == 2), inc_dev ~ reform* unaligned)
lm11_cov <-  lm(data = survey_opp %>%
                filter(mvc_correct == 2), inc_dev ~ reform* unaligned +
                age +female + white + college)
lm12 <- lm(data = survey_opp %>%
           filter(mvc_correct == 2), opp_dev ~ reform* unaligned)

lm12_cov <- lm(data = survey_opp%>%
               filter(mvc_correct == 2), opp_dev ~ reform* unaligned+
               age +female + white + college)
options("modelsummary_format_numeric_latex" = "plain")


table_K2 <-modelsummary(list("Competence" = lm11,
                  "Competence" = lm11_cov,
                  "Veto" = lm12,
                  "Veto" = lm12_cov),
             coef_map = c("reform" = "Reform",
                          "unaligned" = "Opposition",
                          "reform:unaligned" = "Reform * Opposition" ),
             gof_map = c("nobs", "r.squared"),
             stars = TRUE)

print(table_K2)

#########################################
# Appendix L - Table L1
#########################################

lm13 <-  lm(data = survey_inc %>%
           filter(mvc_correct == 2 & comp_check1 == 1 & comp_check2 == 1), prop_belief/100 ~ reform* aligned)
lm13_cov <- lm(data = survey_inc %>%
                filter(mvc_correct == 2& comp_check1 == 1 & comp_check2 == 1), prop_belief/100 ~ reform* aligned +
                age +female + white + college)
lm14 <-  lm(data = survey_inc%>%
           filter(mvc_correct == 2& comp_check1 == 1 & comp_check2 == 1), opp_belief/100 ~ reform* aligned)


lm14_cov <-  lm(data = survey_inc%>%
               filter(mvc_correct == 2& comp_check1 == 1 & comp_check2 == 1), opp_belief/100 ~ reform* aligned+
               age +female + white + college)
options("modelsummary_format_numeric_latex" = "plain")


tab_L1 <- modelsummary(list("Competence" = lm13,
                  "Competence" = lm13_cov,
                  "Veto" = lm14,
                  "Veto" = lm14_cov),
             coef_map = c("reform" = "Reform",
                          "aligned" = "Incumbent",
                          "reform:aligned" = "Reform * Incumbent",
                          "(Intercept)" = "Constant" ),
             gof_map = c("nobs", "r.squared"),
             stars = TRUE)

print(tab_L1)

#########################################
# Appendix L - Table L2
#########################################

lm15 <- lm(data = survey_opp %>%
           filter(mvc_correct == 2 & comp_check1 == 1 & comp_check2 == 1), prop_belief/100 ~ reform* unaligned)
lm15_cov <-  lm(data = survey_opp %>%
                filter(mvc_correct == 2 & comp_check1 == 1 & comp_check2 == 1), prop_belief/100 ~ reform* unaligned +
                age +female + white + college)
lm16 <- lm(data = survey_opp%>%
           filter(mvc_correct == 2 & comp_check1 == 1 & comp_check2 == 1), opp_belief/100 ~ reform* unaligned)


lm16_cov <- lm(data = survey_opp %>%
               filter(mvc_correct == 2 & comp_check1 == 1 & comp_check2 == 1), opp_belief/100 ~ reform* unaligned+
               age +female + white + college)
options("modelsummary_format_numeric_latex" = "plain")

summary(lm2)

tab_L2 <- modelsummary(list("Competence" = lm15,
                  "Competence" = lm15_cov,
                  "Veto" = lm16,
                  "Veto" = lm16_cov),
             coef_map = c("reform" = "Reform",
                          "unaligned" = "Opposition",
                          "reform:unaligned" = "Reform * Opposition",
                          "(Intercept)" = "Constant" ),
             gof_map = c("nobs", "r.squared"),
             stars = TRUE)

print(tab_L2)



#########################################
# Appendix M - Table M1
#########################################

lm17 <- lm(data = survey_inc %>%
           filter(mvc_correct == 2 & time_min > 5 & time_min < 30), prop_belief/100 ~ reform* aligned)
lm17_cov <-  lm(data = survey_inc %>%
                filter(mvc_correct == 2 & time_min > 5 & time_min < 30), prop_belief/100 ~ reform* aligned +
                age +female + white + college)
lm18 <- lm(data = survey_inc%>%
           filter(mvc_correct == 2 & time_min > 5 & time_min < 30), opp_belief/100 ~ reform* aligned)


lm18_cov <- lm(data = survey_inc%>%
               filter(mvc_correct == 2 & time_min > 5 & time_min < 30), opp_belief/100 ~ reform* aligned+
               age +female + white + college)

tab_M1 <- modelsummary(list("Competence" = lm17,
                  "Competence" = lm17_cov,
                  "Veto" = lm18,
                  "Veto" = lm18_cov),
             coef_map = c("reform" = "Reform",
                          "aligned" = "Incumbent",
                          "reform:aligned" = "Reform * Incumbent",
                          "(Intercept)" = "Constant" ),
             gof_map = c("nobs", "r.squared"),
             stars = TRUE)


print(tab_M1)


#########################################
# Appendix M - Table M2
#########################################

lm19 <- lm(data = survey_opp %>%
             filter(mvc_correct == 2 & time_min > 5 & time_min < 30), prop_belief/100 ~ reform* unaligned)
lm19_cov <-  lm(data = survey_opp %>%
                  filter(mvc_correct == 2 & time_min > 5 & time_min < 30), prop_belief/100 ~ reform* unaligned +
                  age +female + white + college)
lm20 <- lm(data = survey_opp%>%
             filter(mvc_correct == 2 & time_min > 5 & time_min < 30), opp_belief/100 ~ reform* unaligned)

lm20_cov <- lm(data = survey_opp %>%
                 filter(mvc_correct == 2 & time_min > 5 & time_min < 30), opp_belief/100 ~ reform* unaligned+
                 age +female + white + college)

tab_M2 <- modelsummary(list("Competence" = lm19,
                            "Competence" = lm19_cov,
                            "Veto" = lm20,
                            "Veto" = lm20_cov),
                       coef_map = c("reform" = "Reform",
                                    "unaligned" = "Opposition",
                                    "reform:unaligned" = "Reform * Opposition",
                                    "(Intercept)" = "Constant" ),
                       gof_map = c("nobs", "r.squared"),
                       stars = TRUE)
print(tab_M2)








